package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.ChanPinXiLie;
import com.ysd.entity.DaiYanTuan;
import com.ysd.util.DBManger;

public class ChanPinXiLieDaoImpl {
	public List<ChanPinXiLie> getChanPinXiLieList(){
		List<ChanPinXiLie> cpxlList = new ArrayList<ChanPinXiLie>();
		 
		 Connection conn = DBManger.getConnection();
		 Statement st= null;
		 ResultSet rs=null;
		 
		 try {
			st= conn.createStatement();
			rs = st.executeQuery("select * from chanPinXiLie");
			while(rs.next()) {
				int cpxlId = rs.getInt("chanpinId");
				String cpxlName=rs.getString("chanPinXiLie");
				ChanPinXiLie cpxl=new ChanPinXiLie(cpxlId,cpxlName);
				cpxlList.add(cpxl);
			}
			return cpxlList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManger.closeAll(conn, st, rs);
		} 	 
	}
	
	/**
	 * 根据分类Id 删除产品分类
	 * cId 要删除产品分类的Id
	 * 返回的是要删除的受影响行数
	 */
	
	public int deleteProductCateById(int cId){
		return DBManger.executeUpdate("DELETE from chanpinxilie where chanpinId = "+cId);
	}
	
	/**
	 * 添加产品系列的方法
	 */
	public int addChanPinXiLie(ChanPinXiLie cpxl) {
		return DBManger.executeUpdate("INSERT into chanpinxilie values(null,'"+cpxl.getChanPinXiLie()+"',null)");
	}
	
	
	/**
	 * 通过产品名称查询分类
	 * @param name
	 * @return
	 */
	public List<ChanPinXiLie> getChanPinXiLieByName(String name){
		List<ChanPinXiLie> pcList = new ArrayList<ChanPinXiLie>();
		
		Connection conn = DBManger.getConnection();
		Statement st=null;
		ResultSet rs= null; 
		try {
			st = conn.createStatement();
			rs =st.executeQuery("SELECT * FROM chanpinxilie where chanpinxilie = '"+name+"'");
			while(rs.next()) {
				int pcId =rs.getInt("chanpinId");
				String pcName =rs.getString("chanPinXiLie");
				
				ChanPinXiLie pc = new ChanPinXiLie(pcId, pcName);
				pcList.add(pc);
				
			}
			return pcList;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManger.closeAll(conn, st, rs);
		}
		
		
	}
	/**
	 * 回显数据
	 * @param Id
	 * @return
	 */
	public ChanPinXiLie getCPById(int Id) {
		Connection conn = DBManger.getConnection();
		Statement st = null; 
		ResultSet rs = null;
		ChanPinXiLie cp = null;
		try {
			st=conn.createStatement();
			rs=st.executeQuery("select * from chanpinxilie where chanpinId = "+Id);
			while(rs.next()){
				int chanpinId=rs.getInt("chanpinId");
				String chanPinXiLie = rs.getString("chanPinXiLie");
				 cp = new ChanPinXiLie(chanpinId, chanPinXiLie);
			}
			return cp;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManger.closeAll(conn, st, rs);
		}
	}
	/**
	 * 修改产品分类
	 * @param name
	 * @return
	 */
	public int updateCPXL(ChanPinXiLie name) {
		String sql = "update chanpinxilie set chanPinXiLie = '"+name.getChanPinXiLie()+"' where chanPinId = "+name.getChanpinId()+"";
		System.out.println(sql);
		return DBManger.executeUpdate(sql);
	}

	
}
